import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:study_app/tools/app_color.dart';

class AutoMaxTextView extends StatefulWidget {
  final String? title;
  final String? hintText;
  final String? defaultValue;
  final void Function(String) onChangeText;
  final Color fillcolor;
  final int? length;

  AutoMaxTextView(
      {this.title,
      this.hintText,
      this.defaultValue,
      this.fillcolor = backgroundColor,
      this.length,
      required this.onChangeText});
  @override
  State<StatefulWidget> createState() => _AutoMaxTextViewState();
}

class _AutoMaxTextViewState extends State<AutoMaxTextView> {
  TextEditingController controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    if ((widget.defaultValue ?? "").isNotEmpty) {
      controller.text = widget.defaultValue!;
    }
    return Container(
      child: _expanded(),
    );
  }

  Expanded _expanded() {
    return Expanded(
      child: _textField(),
    );
  }

  TextField _textField() {
    return TextField(
      controller: controller,
      onChanged: _onChangeText,
      decoration: InputDecoration(
        fillColor: widget.fillcolor,
        filled: true,
        hintText: widget.hintText,
        border: InputBorder.none,
      ),
      keyboardType: TextInputType.multiline,
      maxLines: 100,
      maxLength: widget.length,
    );
  }

  _onChangeText(String text) {
    if (text.length > 3600) {
    } else {
      widget.onChangeText(text);
    }
  }
}
